<?php

  require_once "function.php";

  if($_POST["ChartNo"]==NULL){
      $id=$_GET["id"];
  }else $id=$_POST["ChartNo"];
  if($_POST["durnum"]==NULL){
      $durnum=6;
  }else $durnum=$_POST["durnum"];
  if($_POST["dattype"]==NULL){
      $dattype='M';
  }else $dattype=$_POST["dattype"];
  $img="http://172.16.38.1/DigReport/Xray/XrayExam.asp?reptype=ALL&durnum=".$durnum."&dattype=".$dattype."&ChartNo=".$id;

  $PACS='
      <form name=formpacsimage action="http://pacsweb.ntuh.gov.tw/StudyViewerServlet" target="MainPACS" method=post>
      <input type=hidden name=user value=epr>
      <input type=hidden name=password value=epr>
      <input type=hidden name=accession_number value=T0099492355^>
      <input type=hidden name=patient_id value=5460637^>
      <input type=hidden name=width value=100%><input type=hidden name=height value=100%>
      </form>
      <script language="javascript">formpacsimage.submit();</script>';

  if($_GET['display']==TRUE||$_POST['display']=='v'){
    if($_POST['display']=='v'&&$_POST['check']!=NULL){
      foreach($_POST['check'] as $acc){
        if($acc!=NULL){
            $acc_group=$acc_group.$acc.'^';
            $id_group=$id_group.$id.'^';
        }
      }
    }    
    if($_GET['display']==TRUE){
      $acc_group=$_GET['accessno'].'^';
      $id_group=$_GET['id'].'^';
    }
    if($acc_group!=NULL){
      $pacsDom=new DOMDocument();
      $pacsDom->preserveWhiteSpace=FALSE;
      $pacsDom->loadHTML($PACS);
      foreach($pacsDom->getElementsByTagName('input') as $element){
        if($element->getAttribute('name')=='patient_id')
          $element->setAttribute('value',$id_group);
        if($element->getAttribute('name')=='accession_number')
          $element->setAttribute('value',$acc_group);
      }
      echo $pacsDom->saveHTML();
    }
  }

  // Replace form action
  $imgDom=ntuhDom($img);
  foreach($imgDom->getElementsByTagName('form') as $element){
      $element->setAttribute('action','PACS.php');
  }

  $access=array();
  $rpturl=array();
  $rpt_a=array();
  $pacsurl=array();
  foreach($imgDom->getElementsByTagName('tr') as $idx => $element){
      if($element->childNodes->item(15)!=NULL){
          if($idx<=3) continue;
          $element->removeChild($element->childNodes->item(8));
          $accessno=trim($element->childNodes->item(11)->nodeValue,'　');
          $element->removeChild($element->childNodes->item(11));
          $access[$idx]=$imgDom->createElement('input');
          $access[$idx]->setAttribute('type','checkbox');
          $access[$idx]->setAttribute('name','check[]');
          $box=$element->childNodes->item(9);
          $rpt=$element->childNodes->item(12);
          $pacs_a=$element->childNodes->item(6);
          if($idx!=4){
            $box->replaceChild($access[$idx],$box->childNodes->item(1));
            $box->removeChild($box->firstChild);
            $rpt_a[$idx]=$imgDom->createElement('a');
            $rpturl[$idx]=$element->childNodes->item(6)->childNodes->item(1)->getAttribute('href');
            @$rpt_a[$idx]->setAttribute('href','http://172.16.38.1/DigReport/Xray/'.mb_convert_encoding($rpturl[$idx],"big5","utf-8"));
            while($rpt->childNodes->length>=2){
                $rpt->removeChild($rpt->firstChild);
            }
            $rpt_a[$idx]->nodeValue=trim($rpt->nodeValue,'　 ');
            $element->childNodes->item(12)->replaceChild($rpt_a[$idx],$rpt->firstChild);
            $pacs_a->childNodes->item(1)->setAttribute('href','PACS.php?display=1&id='.$id.'&accessno='.$accessno);
            $access[$idx]->setAttribute('value',$accessno);
          }else{
             $access[$idx]->setAttribute('name','display');
             $access[$idx]->setAttribute('value','v');
             $access[$idx]->setAttribute('onclick','document.FrontPage_Form1.submit()');
             $access[$idx]->nodeValue='v';
             $box->replaceChild($access[$idx],$box->firstChild);
          }
          //$access[$idx]->setAttribute('value',$accessno);
      }
  }
  foreach($imgDom->getElementsByTagName('img') as $element){
    $element->parentNode->parentNode->removeChild($element->parentNode);
  }
  $result=str_replace('</form>','',$imgDom->saveHTML());
  $result=$result.'</form>';
  echo $result;
?>
